// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: Implementation of CErrorReportAction.
//   $Id$


#include "stdafx.h"
#include "ErrorReportAction.h"
#include <dom2Events.h>
#include "events.h"

CErrorReportAction * CErrorReportActionFactory::CreateErrorReportAction(const BSTR sError, IHTMLElement * pMessageElement)
{
	CErrorReportAction * pAction = 0;
	//Check the given values.  if either invalid, just return 0;
	if(!(::isEmpty(sError) || !pMessageElement))
	{
		//Construct a new object, using the given arguments
		pAction = new CErrorReportAction(sError,pMessageElement);
		
		//insert the new object into the list of deletable objects.
		if(pAction)
		{
			sm_ErrorActions.push_back(pAction);
		}
	}
	return pAction;
}

void CErrorReportActionFactory::Clear()
{
	//Loop through sm_ErrorActions, removing and deleting as we go. 
	while(!sm_ErrorActions.empty())
	{
		CErrorReportAction * pAction = sm_ErrorActions.front();
		delete pAction;
		sm_ErrorActions.pop_front();
	}
}

HRESULT CErrorReportAction::Invoke()
{
	HRESULT hr;
	//Get the element as an eventTarget.
	//	TODO: perhaps this check should be in CreateErrorReportAction?
	CComQIPtr<IEventTarget> pTarget(m_pMessageElement);
	if(pTarget)
	{
		CComPtr<IDOMEvent> evt;
		hr = evt.CoCreateInstance(CLSID_DOMEvent);
		if(evt)
		{
			//write the error message into the message element
			m_pMessageElement->put_innerHTML(m_sError);

			//dispatch the DOMActivate event to display the error message.
			
			evt->initEvent(EVENT_ACTIVATE,TRUE,TRUE);
			BOOL bCancelled;
			hr = pTarget->dispatchEvent(evt,&bCancelled);
		}
	}
	else
	{
		hr = E_NOINTERFACE;
	}
	return hr;
};
